package com.ysd.lis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.*;
import com.ysd.lis.mapper.*;
import com.ysd.lis.mapper.sys.SysOrgDepartmentMapper;
import com.ysd.lis.mapper.sys.SysOrganizationMapper;
import com.ysd.lis.request.*;
import com.ysd.lis.service.*;
import com.ysd.lis.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author peng
 * @since 2021-10-08
 */
@Service
@Slf4j
public class TEmployeeServiceImpl extends ServiceImpl<TEmployeeMapper, TEmployee> implements TEmployeeService {

    @Autowired
    SysOrganizationMapper sysOrganizationMapper;
    @Autowired
    SysOrgDepartmentMapper sysOrgDepartmentMapper;
    @Autowired
    TEmployeeMapper tEmployeeMapper;
    @Autowired
    TEmployeeDeptGroupMapper tEmployeeDeptGroupMapper;
    @Autowired
    TEmployeeDeptGroupService tEmployeeDeptGroupService;
    @Override
    public Result findAllEmployeeListByOrgDept(EmployeePageParam employeePageParam) {
        String orgDeptId = employeePageParam.getOrgDeptId();
        //1.机构 2.部门
        String orgDeptType = employeePageParam.getOrgDeptType();
        Page<SysEmployeeVo> page = new Page<>();
        page.setCurrent(employeePageParam.getPageIndex());
        page.setSize(employeePageParam.getPageSize());
        if(orgDeptType.equals("1")){
            //查询此机构及机构下的所有子机构
            List<SysOrganization>  orgList = sysOrganizationMapper.getOrgByPid(orgDeptId);
            List<String> orgIds = orgList.stream().map(SysOrganization::getId).collect(Collectors.toList());
            employeePageParam.setOrgIds(orgIds);
        }else if(orgDeptType.equals("2")){
            //查询此部门及部门下所有的子部门
            List<SysOrgDepartment> deptList = sysOrgDepartmentMapper.getDeptByPid(orgDeptId);
            List<String> deptIds = deptList.stream().map(SysOrgDepartment::getId).collect(Collectors.toList());
            employeePageParam.setDeptIds(deptIds);
        }
        Page<SysEmployeeVo> employeePage =  tEmployeeMapper.getEmployeePageList(page,employeePageParam);
        List<SysEmployeeVo> employeeVoList = employeePage.getRecords();
        if(employeeVoList.size()>0){
            List<String> eIds = employeeVoList.stream().map(SysEmployeeVo::getId).collect(Collectors.toList());
            //获取人员的部门小组
            LambdaQueryWrapper<TEmployeeDeptGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(TEmployeeDeptGroup::getEmployeeId,eIds).eq(TEmployeeDeptGroup::getDelFlag,'0');
            List<TEmployeeDeptGroup> employeeDeptGroupList = tEmployeeDeptGroupMapper.selectList(lambdaQueryWrapper);
            for(TEmployeeDeptGroup group : employeeDeptGroupList){
                int indexOf = eIds.indexOf(group.getEmployeeId());
                if(indexOf>=0){
                    SysEmployeeVo tEmployeeVo = employeeVoList.get(indexOf);
                    List<TEmployeeDeptGroup> groupList = tEmployeeVo.getEmployeeDeptGroupList();
                    if(null == groupList){
                        groupList = new ArrayList<>();
                        tEmployeeVo.setEmployeeDeptGroupList(groupList);
                    }
                    groupList.add(group);
                }
            }
        }
        return Result.succ(1, "查询成功", employeePage);
    }

    @Override
    public Result addEmployee(EmployeeAddParam employeeAddParam) {
        TEmployee tEmployee = new TEmployee();
        BeanUtil.copyProperties(employeeAddParam,tEmployee);
        int insert = tEmployeeMapper.insert(tEmployee);
        if(insert>0){
            //插入人员部门小组中间表
             List<String> groupIds = employeeAddParam.getGroupIds();

            if (CollectionUtils.isNotEmpty(groupIds)&&StringUtils.isNotBlank(employeeAddParam.getDeptId())){
                List<TEmployeeDeptGroup> employeeDeptGroupList = new ArrayList<>();
                groupIds.forEach(a->{
                    TEmployeeDeptGroup employeeDeptGroup = new TEmployeeDeptGroup();
                    employeeDeptGroup.setEmployeeId(tEmployee.getId());
                    employeeDeptGroup.setGroupId(a);
                    employeeDeptGroupList.add(employeeDeptGroup);
                });
                tEmployeeDeptGroupService.saveBatch(employeeDeptGroupList);
            }

            return Result.succ(1,"保存成功",tEmployee);
        }else {
            return Result.fail("保存失败");
        }
    }

    @Override
    public Result editEmployee(EmployeeAddParam employeeAddParam) {
        TEmployee tEmployee = new TEmployee();
        BeanUtil.copyProperties(employeeAddParam,tEmployee);
        int i = tEmployeeMapper.updateById(tEmployee);
        if(i>0){
            //删除用户部门小组中间表
            LambdaQueryWrapper<TEmployeeDeptGroup> delGroupWrapper = new LambdaQueryWrapper<>();
            delGroupWrapper.eq(TEmployeeDeptGroup::getEmployeeId,employeeAddParam.getId());
            tEmployeeDeptGroupMapper.delete(delGroupWrapper);
            //插入用户部门小组中间表
            List<String> groupIds = employeeAddParam.getGroupIds();
            if (CollectionUtils.isNotEmpty(groupIds)&&StringUtils.isNotBlank(employeeAddParam.getDeptId())){
                List<TEmployeeDeptGroup> groupList = new ArrayList<>();
                groupIds.forEach(a->{
                    TEmployeeDeptGroup group = new TEmployeeDeptGroup();
                    group.setEmployeeId(employeeAddParam.getId());
                    group.setGroupId(a);
                    groupList.add(group);
                });
                tEmployeeDeptGroupService.saveBatch(groupList);
            }
            return Result.succ(1,"保存成功",tEmployee);
        }else {
            return Result.fail("保存失败");
        }
    }

    @Override
    public Result deleteEmployeeById(String id) {
        int d = tEmployeeMapper.deleteById(id);
        if(d>0){
            //删除用户分组表
            LambdaQueryWrapper<TEmployeeDeptGroup> delGroupWrapper = new LambdaQueryWrapper<>();
            delGroupWrapper.eq(TEmployeeDeptGroup::getEmployeeId,id);
            tEmployeeDeptGroupMapper.delete(delGroupWrapper);
            return Result.succ(1,"删除成功",null);
        }else {
            return Result.fail("删除失败");
        }
    }
}
